package com.gjy.springboot.verify;

import lombok.Data;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.ConfigurablePropertyAccessor;
import org.springframework.beans.PropertyAccessorFactory;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-02-07 15:26:21
 */
public class BeanWrapperTest {

    @Test
    public void test1() {
        User user = new User();
        BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(user);
        beanWrapper.setPropertyValue("username","张三");
        System.out.println("user = " + user);
        beanWrapper.setPropertyValue("username","李四");
        System.out.println("user = " + user);
    }

    @Test
    public void test2() {
        User user = new User();
        ConfigurablePropertyAccessor fieldAccess = PropertyAccessorFactory.forDirectFieldAccess(user);
        fieldAccess.setPropertyValue("username","张三");
        System.out.println("user = " + user);
        fieldAccess.setPropertyValue("username","李四");
        System.out.println("user = " + user);
    }

    @Data
    private static final class User {
        private String username;
    }
}
